'----------------------------------------------
' Name: Ali Karbassi & Jeremy Nieto
' Date: February 26, 2007
' Description: Project 3
'              Form formForHouse
'----------------------------------------------

Imports UWPCS3340

Public Class formForHouse

   Friend houseList As ArrayList

   ''' <summary>
   ''' Clears the form. This will clear all previous values on the form and the user can enter values for a 
   ''' new house. Button cmdModify will be disabled, while all other buttons will be enabled.
   ''' </summary>
   ''' <remarks></remarks>
   Public Sub clearForm()

      allErrors.Clear()
      txtID.Clear()
      txtID.ReadOnly = False
      txtPrice.Clear()
      cboHouse.Enabled = True
      cboHouse.SelectedIndex = -1
      rdoRooms2.Checked = False
      rdoRooms3.Checked = False
      rdoRooms4.Checked = False
      rdoGarage1.Checked = False
      rdoGarage2.Checked = False
      rdoGarage3.Checked = False

      ' Disable Modify Button
      cmdModify.Enabled = False

      ' Enable the rest of the buttons.
      cmdSave.Enabled = True
      cmdClear.Enabled = True
      cmdSave.Enabled = True
      cmdUnload.Enabled = True

   End Sub

   ''' <summary>
   ''' Calls clearForm which will clear the form.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
   cmdClear.Click

      clearForm()

   End Sub

   ''' <summary>
   ''' Clicking cmdSave will save a new house to the list of houses. Only when the type is "Madison," the 
   ''' click event procedure should check the ID (not any other values) in order to call the constructor with
   ''' a parameter. The client code does not validate any other values for the house, but must catch any error
   ''' raised by the class code. After a new house is saved successfully, button cmdSave will be disabled, 
   ''' while other buttons will be enabled.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click

      Dim valid As Boolean = True
      Dim cboValid As Boolean = True

      Dim found As Boolean = False
      Dim h As House = New House

      allErrors.Clear()

      ' Check to see if combobox has been selected.
      If cboHouse.SelectedIndex = -1 Then

         allErrors.SetError(cboHouse, "Select a type first!")
         cboValid = False

      End If

      If cboValid = False Then
         Try

            Select Case cboHouse.Text
               Case "Madison"
                  h = New Madison(txtID.Text)

               Case "Platteville"
                  h = New Platteville

               Case "Chicago"
                  h = New Chicago

            End Select

            If Not cboHouse.Text = "Madison" Then
               h.ID = txtID.Text
            End If

         Catch ex As Exception

            allErrors.SetError(txtID, ex.Message)
            valid = False

         End Try
      Else

         Try

            Select Case cboHouse.Text
               Case "Madison"
                  h = New Madison(txtID.Text)

               Case "Platteville"
                  h = New Platteville

               Case "Chicago"
                  h = New Chicago

            End Select

            If Not cboHouse.Text = "Madison" Then
               h.ID = txtID.Text
            End If

         Catch ex As Exception

            allErrors.SetError(txtID, ex.Message)
            valid = False

         End Try

         ' Check to see if already added.
         For i As Integer = 0 To houseList.Count - 1
            If txtID.Text = CType(houseList.Item(i), House).ID Then
               found = True
            End If
         Next

         If found = True Then
            allErrors.SetError(txtID, "This ID already exists!")
            valid = False
         End If


         Try

            AddHandler h.Changed, AddressOf HandlesChanged

            Dim valid2 As Boolean = True

            Try

               If rdoRooms2.Checked Then
                  h.Rooms = 2
               ElseIf rdoRooms3.Checked Then
                  h.Rooms = 3
               ElseIf rdoRooms4.Checked Then
                  h.Rooms = 4
               Else
                  h.Rooms = -1
               End If
               h.setRooms()

            Catch ex As Exception

               allErrors.SetError(gboNumberOfRooms, ex.Message)
               valid2 = False

            End Try

            If rdoGarage1.Checked Then
               h.Garages = 1
            ElseIf rdoGarage2.Checked Then
               h.Garages = 2
            ElseIf rdoGarage3.Checked Then
               h.Garages = 3
            Else
               h.Garages = -1
            End If

            h.setGarages()

            If valid2 = True And valid = True Then
               h.setPrice()

               txtPrice.Text = Format(h.Price.ToString, "currency")

               houseList.Add(h)

               ' Disable things
               txtID.ReadOnly = True
               txtID.BackColor = Color.White
               cmdSave.Enabled = False
               cmdModify.Enabled = True
               cboHouse.Enabled = False

            End If

         Catch ex As Exception

            If ex.Source = "id" Then
               allErrors.SetError(txtID, ex.Message)
            End If

            If ex.Source = "rooms" Then
               allErrors.SetError(gboNumberOfRooms, ex.Message)
            End If

            If ex.Source = "garages" Then
               allErrors.SetError(gboNumberOfGarages, ex.Message)
            End If

         End Try

      End If

   End Sub

   ''' <summary>
   ''' Clicking cmdExit will terminate the project.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub cmdUnload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
   cmdUnload.Click

      Me.Close()

   End Sub

   ''' <summary>
   ''' Clicking cmdModify will save the changes made to the house displayed on the form. The client code
   ''' should just call the class methods without checking the input and catch any possible error. When the
   ''' number of rooms and/or the number of garages of a house has been modified, the price will be modified
   ''' accordingly and the new price will be displayed on the form.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub cmdModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
   cmdModify.Click

      allErrors.Clear()

      Try

         Dim h As House = Nothing

         For i As Integer = 0 To houseList.Count - 1 Step 1
            If CType(houseList.Item(i), House).ID = txtID.Text Then
               h = CType(houseList.Item(i), House)
            End If
         Next

         Dim valid As Boolean = True

         Try

            If rdoRooms2.Checked Then
               h.Rooms = 2
            ElseIf rdoRooms3.Checked Then
               h.Rooms = 3
            ElseIf rdoRooms4.Checked Then
               h.Rooms = 4
            Else
               h.Rooms = -1
            End If
            h.setRooms()

         Catch ex As Exception

            allErrors.SetError(gboNumberOfRooms, ex.Message)
            valid = False

         End Try

         If rdoGarage1.Checked Then
            h.Garages = 1
         ElseIf rdoGarage2.Checked Then
            h.Garages = 2
         ElseIf rdoGarage3.Checked Then
            h.Garages = 3
         Else
            h.Garages = -1
         End If

         h.setGarages()

         If valid = True Then
            h.setPrice()

            txtPrice.Text = Format(h.Price.ToString, "currency")
         End If

      Catch ex As Exception

         If ex.Source = "id" Then
            allErrors.SetError(txtID, ex.Message)
         End If

         If ex.Source = "rooms" Then
            allErrors.SetError(gboNumberOfRooms, ex.Message)
         End If

         If ex.Source = "garages" Then
            allErrors.SetError(gboNumberOfGarages, ex.Message)
         End If

      End Try

   End Sub

   ''' <summary>
   ''' Sets the houseList to a new ArrayList
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub formForHouse_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
   MyBase.Load

      houseList = New ArrayList

   End Sub

   ''' <summary>
   ''' Handles the ID if it was changed.
   ''' </summary>
   ''' <param name="ID"></param>
   ''' <remarks></remarks>
   Public Sub HandlesChanged(ByVal ID As String)

      MsgBox("The price for house " & ID & " has changed!", MsgBoxStyle.OkOnly)

   End Sub

End Class
